package com.sk.sourcecircle.activity.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.adapter.PepAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.PepListBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/11/19.
 */

public class AddFriendActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.ed_search)
    EditText edSearch;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private int page;
    private int count;
    private String searchString = "";
    PepAdapter adapter;
    private List<PepListBean.DataBeanX.DataBean> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;


    @Override
    protected int getContentView() {
        return R.layout.activity_search_friend;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new PepAdapter(R.layout.item_pep,allDataList);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

        edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if (edSearch.getText().toString().length() > 0) {
                        page = 1;
                        allDataList.clear();
                        searchString = edSearch.getText().toString();
                        AppApi.searchPep(1,10,searchString,AddFriendActivity.this,0,AddFriendActivity.this);
                    } else {
                        Toast.makeText(mContext, "搜索名称不能为空!", Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });

    }

    @OnClick(R.id.cancel)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {

        PepListBean pepListBean = JSON.parseObject(result,PepListBean.class);
        List<PepListBean.DataBeanX.DataBean> dataBeanList = pepListBean.getData().getData();
        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();


    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext,PersonDetailsActivity.class);
        intent.putExtra("id",allDataList.get(position).getUser_id());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.searchPep(1,10,searchString,AddFriendActivity.this,0,AddFriendActivity.this);
    }
}
